package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonAreaAsyGet;

import java.util.List;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public abstract class CityAdapter extends BaseExpandableListAdapter {

    public LayoutInflater layoutInflater;

    public JsonAreaAsyGet.Info info;

    public CityAdapter(Context context, JsonAreaAsyGet.Info info){

        layoutInflater = ((Activity)context).getLayoutInflater();

        this.info = info;

    }

    @Override
    public int getGroupCount() {

        return info.letters.size();

    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return info.areaMap.get(info.letters.get(groupPosition)).size();

    }

    @Override
    public Object getGroup(int groupPosition) {

        return null;

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return null;

    }

    @Override
    public long getGroupId(int groupPosition) {

        return 0;

    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return 0;

    }

    @Override
    public boolean hasStableIds() {

        return true;

    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        TextView title;

        if(convertView == null){

            convertView = QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_city_group, null));

            convertView.setTag(title = (TextView) convertView.findViewById(R.id.item_city_group_title));

        }else{

            title = (TextView) convertView.getTag();

        }

        title.setText(info.letters.get(groupPosition));

        return convertView;

    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        TextView title;

        if(convertView == null){

            convertView = QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_city_child, null));

            convertView.setTag(title = (TextView) convertView.findViewById(R.id.item_city_child_title));

        }else{

            title = (TextView) convertView.getTag();

        }

        final List<JsonAreaAsyGet.Area> list = info.areaMap.get(info.letters.get(groupPosition));

        if(list.size() == 1 || childPosition == list.size() - 1){

            title.setBackgroundColor(Color.parseColor("#ffffff"));

        }else{

            title.setBackgroundResource(R.drawable.layer_solid_white_bottom_grey);

        }

        title.setText(list.get(childPosition).name);

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                onItemClick(list.get(childPosition));

            }

        });

        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return false;

    }

    protected abstract void onItemClick(JsonAreaAsyGet.Area area);

}
